
from typing import Any

class FieldInfo:
    """字段信息类"""
    def __init__(self, name: str, type_name: str, is_primary: bool = False, is_nullable: bool = True, default_value: Any = None, comment: str = ""):
        self.name = name
        self.type_name = type_name
        self.is_primary = is_primary
        self.is_nullable = is_nullable
        self.default_value = default_value
        self.comment = comment

        # 转换为Python类型
        self.type = self._get_python_type()

    def _get_python_type(self) -> str:
        """将MySQL类型转换为Python类型"""
        type_lower = self.type_name.lower()

        if 'int' in type_lower:
            return 'int'
        elif 'float' in type_lower or 'double' in type_lower or 'decimal' in type_lower:
            return 'float'
        elif 'varchar' in type_lower or 'text' in type_lower or 'char' in type_lower or 'tinytext' in type_lower or 'mediumtext' in type_lower or 'longtext' in type_lower:
            return 'str'
        elif 'date' in type_lower:
            return 'date'
        elif 'datetime' in type_lower or 'timestamp' in type_lower:
            return 'datetime'
        elif 'bool' in type_lower:
            return 'bool'
        elif 'json' in type_lower:
            return 'dict'
        else:
            return 'Any'

    def to_camel_case(self) -> str:
        """将下划线命名转换为驼峰命名"""
        components = self.name.split('_')
        return ''.join(x.capitalize() for x in components)
